package com.tianpengtech.admin.fairwithdraw;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.activerecord.Record;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairbankno.FairBankNo;
import com.tianpengtech.common.model.fairwithdraw.FairWithdraw;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.util.JsonUtil;

import static com.tianpengtech.common.model.opertion.Opertion.OPT_FAIR_GET_CASH;

/**
* 路由配置地址：add("/admin/fairwithdraw", FairWithdrawAdminController.class, "/admin/fairwithdraw");
* 数据库映射地址：arp.addMapping(FairWithdraw.dao.tableName(),FairWithdraw.class);
*/
@Before(InitInterceptor.class)
public class FairWithdrawAdminController extends AbstractAdminController{
	private IAdminOpt opt = (IAdminOpt) new FairWithdrawAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=FairWithdraw.class;
		this.service=new FairWithdrawAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	@Override
	public void parsePageRecord(Object results) {
		FairWithdraw fairWithdraw = (FairWithdraw)results;
		long bankId = fairWithdraw.getInt(FairWithdraw.BANK_ID);
		FairBankNo bankNo = FairBankNo.dao.findById(bankId);
		if(bankNo!=null){
			Fair fair= Fair.dao.findById(fairWithdraw.getInt(FairWithdraw.WH_ID));
			fairWithdraw.set(FairWithdraw.BANK_ID, bankNo.getStr(FairBankNo.BANK));
			//真实姓名
			fairWithdraw.put("real_name",bankNo.getStr(FairBankNo.REAL_NAME));
			//余额
			fairWithdraw.put("balance",fair.getDouble(fair.MONEY));
			fairWithdraw.put("avaliable_fee",fair.getDouble(Fair.AVALIABLE_FEE));
			fairWithdraw.set(FairWithdraw.WH_ID, fair.getStr(Fair.NICK_NAME));
			fairWithdraw.set(FairWithdraw.STATUS, fairWithdraw.status());
			fairWithdraw.put("platform_fee", fairWithdraw.platFormFee());
		}
	}
	
	/**
	 * 审核不通过
	 */
	@Before(Tx.class)
	public void  unpass(){
		FairWithdraw fairWithdraw = getModel(FairWithdraw.class);
		String content = getPara(Opertion.CONTENT);
		Kv kv = Kv.by(Opertion.CONTENT, content);
		try {
			opt.changeStatus(fairWithdraw, fairWithdraw.getInt(FairWithdraw.ID), FairWithdraw.STATUS_ERROR, kv);
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	
	/**
	 * 审核通过
	 */
	@Before(Tx.class)
	public void  pass(){
		int id = getParaToInt(FairWithdraw.ID,0);
		try {
			opt.changeStatus(getModel(FairWithdraw.class), id, FairWithdraw.STATUS_SUCCESS, Kv.create());
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	/**
	 * 拒绝理由填写页面
	 */
	public void  toRefuse(){
		int id = getParaToInt(FairWithdraw.ID,0);
		String sql = "select * from fair_admin_opt_log where opt_type=? and obj_id=?";
		Opertion opertion = Opertion.dao.findFirst(sql, OPT_FAIR_GET_CASH, id);
		if (opertion != null) setAttr(Opertion.CONTENT, opertion.getStr(Opertion.CONTENT));
		setAttr("fairWithdraw",this.service.queryById(getModel(FairWithdraw.class), id));
		renderTemplate("fairwithdraw-reason.html");
	}
}

	